package com.eternal.base.utils;

import com.eternal.base.global.BluetoothKey;

import java.util.Arrays;

/**
 * Created by Administrator
 * On 2020/4/26
 * Description:
 * @author Administrator
 */
public class ByteUtils {

    public static String bytes2HexString(byte[] bytes){
        StringBuilder ret = new StringBuilder();
        if (bytes != null) {
            for (byte aByte : bytes) {
                String hex = Integer.toHexString(aByte & 0xFF);
                if (hex.length() == 1) {
                    hex = "0x0" + hex + ",";
                } else {
                    hex = "0x" + hex + ",";
                }
                ret.append(hex);
            }
        }
        return ret.toString();
    }

    public static String bytes2HexString(byte[] bytes, int pos, int len){
        StringBuilder ret = new StringBuilder();
        if (bytes != null) {
            for (int i=pos; i<pos+len; ++i) {
                String hex = Integer.toHexString(bytes[i] & 0xFF);
                if (hex.length() == 1) {
                    ret.append("0x0")
                            .append(hex)
                            .append(',');
                } else {
                    ret.append("0x")
                            .append(hex)
                            .append(',');
                }
            }
        }
        return ret.toString();
    }

    /**
     * 获得某个字节的某一位 是否为0
     * 0为true 1为false
     *
     * @param b 字节
     * @param i 位置
     * @return 是否为0
     */
    public static boolean getBit(byte b, int i) {
        return ((b >> (7 - i)) & 0x1)==0;
    }

    /**
     * b为传入的字节，start是起始位，length是长度，如要获取bit0-bit4的值，则start为0，length为5
     *
     * @param b 字节
     * @param start 起始位
     * @param length 长度
     * @return 值
     */
    public static int getBits(byte b, int start, int length) {
        return ((b >> (8 - start - length))) & (0xFF >> (8 - length));
    }

    /**
     * 字节数组转为有符号的short
     *
     * @param arr 数组
     * @param index 起始位置
     * @return short
     */
    public static short getShort(byte[] arr, int index) {
        return (short) (0xff00 & arr[index] << 8 | (0xff & arr[index + 1]));
    }

    public static char getChar(byte[] arr, int index) {
       return ((char) (((arr[index] & 0xff) << 8) | (arr[index + 1] & 0xff)));
    }

    public static int getInt(byte[] arr, int index) {
       return char2Int(getChar(arr,index));
    }

    /**
     * int转 两位的byte数组
     *
     * @param x 值
     * @return byte[]
     */
    public static byte[] int2ByteArray(int x) {
        byte[] bytes = new byte[2];
        byte hei = (byte) ((x >> 8) & 0xff);
        byte low = (byte) (x & 0xff);
        bytes[0] = hei;
        bytes[1] = low;
        return bytes;
    }

    public static int char2Int(char ch) {
       byte bh= (byte) ((ch&0xFF00)>>8);
       byte bl =  (byte)(ch&0x00FF);
       return  (short) (0xff00 & bh << 8 | (0xff & bl));

//        int x = (int)ch;
//        if (x > 0x7FFF && x != BluetoothKey.EMPTY) {
//            x -= 0x10000;
//        }
//        return x;
    }

    public static char int2Char(int x) {
        byte bh= (byte) ((x&0xFF00)>>8);
        byte bl =  (byte)(x&0x00FF);
        return  (char) (0xff00 & bh << 8 | (0xff & bl));
//
//        if (x<0) {
//            x += 0x10000;
//        }
//        return (char)x;
    }

}
